package com.ruoyi.app.controller;

import com.ruoyi.app.domain.ExamAppointment;
import com.ruoyi.app.domain.ExamProject;
import com.ruoyi.app.domain.HospitalArea;
import com.ruoyi.app.domain.Patient;
import com.ruoyi.app.service.*;
import com.ruoyi.app.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RequestMapping("examAppointmentA")
@RestController
public class ExamAppointmentController {
    @Autowired
    ExamAppointmentService examAppointmentService;
    @Autowired
    PatientService patientService;
    @Autowired
    ExamProjectService examProjectService;
    @Autowired
    HospitalAreaService hospitalAreaService;
    @Autowired
    private BalanceService balanceService;
    @RequestMapping("saveExamAppointment")
    public void saveExamAppointment(@RequestBody ExamAppointment examAppointment) {
        //添加预约
        examAppointment.setReportTime(new Date());
        examAppointment.setAppointmentDate(new Date());
        examAppointment.setStatus(0);
        examAppointmentService.saveExamAppointment(examAppointment);
        //购买成功减少卡金额
        balanceService.Updaterecharge(examAppointment.getPatientId(), examAppointment.getResult());
    }

    @RequestMapping("list")
    public Result list(Integer states) {
        List<ExamAppointment> list = examAppointmentService.list(states);
        for (ExamAppointment examAppointment : list) {
            examAppointment.setPatientName(patientService.getById(Math.toIntExact(examAppointment.getPatientId())).getName());
            examAppointment.setProjectName(examProjectService.getById(Math.toIntExact(examAppointment.getPackageId())).getProjectName());
        }
        //select * from exam_appointment
        return Result.ok(list);
    }


    @RequestMapping("getById")
    public ExamAppointment getById(Integer id) {
        ExamAppointment examAppointment = examAppointmentService.getById(id);

        ExamProject byId1 = examProjectService.getById(Math.toIntExact(examAppointment.getPackageId()));
        examAppointment.setProjectName(byId1.getProjectName());
        examAppointment.setProjectPrice(byId1.getProjectPrice());

        Patient byId = patientService.getById(Math.toIntExact(examAppointment.getPatientId()));
        examAppointment.setPatientName(byId.getName());
        examAppointment.setProjectPhone(byId.getPhone());

        HospitalArea hospitalArea = hospitalAreaService.getById(id);
        examAppointment.setHospitalAddress(hospitalArea.getAddress());
        examAppointment.setHospitalAreaAddress(hospitalArea.getName());
        return examAppointment;
    }

    @RequestMapping("updateExamApp")
    public void updateExamApp(@RequestBody ExamAppointment examAppointment) {
        examAppointment.setStatus(2);
        examAppointmentService.updateExamApp(examAppointment);
    }

    @RequestMapping("deleteExamApp")
    public Result deleteExamApp(Long id) {
        examAppointmentService.deleteExamApp(id);
        return Result.ok("删除成功");
    }
}
